/**
 *Copyright (c) 2024 - 2025 Midea Company of Midea Group.
 *
 * File Name 	    : ui_common.c
 * Author 	        : huocy
 * Date:            : 2024-12-25 15:01:39
 * Des:             :
 */
#include "ui_common.h"

#include "../../pt_printk.h"

void ui_printf(const char *format, ...){
     



}

int midea_ui_safe_set_font(lv_obj_t* obj, FONT_TYPE type, uint16_t weight)
{
    if (!obj) {
        return -1;
    }
    lv_font_t* font = find_font(type, weight);
    if (!font) {
        return -1;
    }
    inc_font_refer(font);
    lv_obj_set_style_text_font(obj, font, LV_PART_MAIN | LV_STATE_DEFAULT);
    return 0;
}

bool midea_ui_safe_add_flag(lv_obj_t* obj, lv_obj_flag_t f)
{
    if (!obj) {
        return false;
    }
    if (lv_obj_has_flag(obj, f)) {
        return false;
    }
    lv_obj_add_flag(obj, f);
    return true;
}

bool midea_ui_safe_del_obj(lv_obj_t* obj)
{
    if (!obj) {
        return false;
    }
    lv_obj_del(obj);
    obj = NULL;
    return true;
}

bool midea_ui_safe_clear_flag(lv_obj_t* obj, lv_obj_flag_t f)
{
    if (!obj) {
        return false;
    }
    if (!lv_obj_has_flag(obj, f)) {
        return false;
    }
    lv_obj_clear_flag(obj, f);
    return true;
}

void midea_get_picfile_path(char* path, int pathLen, char* name)
{
    if (path == NULL || name == NULL) {
        return;
    }
    memset(path, 0, pathLen);
    snprintf(path, pathLen, "A:%s%s", MIDEA_RES_PATH_PREFIX, name);
}

static uint8_t midea_ui_utf8_code_bytes(uint8_t byte)
{
    uint8_t bytes = 0;

    if (byte <= 0x7F) { // then ASCII 占用1个字节
        bytes = 1;
    } else if (byte >= 0xC0 && byte <= 0xDF) { // then 首字节   UTF-8 占用2个字节
        bytes = 2;
    } else if (byte >= 0xE0 && byte <= 0xEF) { // then 首字节   UTF-8 占用3个字节
        bytes = 3;
    } else if (byte >= 0xF0 && byte <= 0xF7) { // then 首字节   UTF-8 占用4个字节
        bytes = 4;
    } else if (byte >= 0xF8 && byte <= 0xFB) { // then 首字节   UTF-8 占用5个字节
        bytes = 5;
    } else if (byte >= 0xFC && byte <= 0xFD) { // then 首字节   UTF-8 占用6个字节
        bytes = 6;
    } else if (byte > 0x7F && byte < 0xC0) { // then UTF-8   非首字节
        bytes = 0;
    }
    return bytes;
}

int midea_ui_util_get_wstr_len(char* name_str)
{
    int ret = 0;

    if (name_str) {
        int len = strlen(name_str);
        for (int i = 0; i < len;) {
            uint8_t bytes = midea_ui_utf8_code_bytes(name_str[i]);
            if (bytes == 0) {
                break;
            }
            ret++;
            i += bytes;
        }
    }
    return ret;
}

int midea_ui_safe_set_size(lv_obj_t* obj, lv_coord_t w, lv_coord_t h)
{
    if (!obj) {
        return false;
    }
    lv_obj_set_size(obj, w, h);
    return true;
}

int midea_ui_safe_obj_align(lv_obj_t* obj, lv_align_t align, lv_coord_t x_ofs, lv_coord_t y_ofs)
{
    if (!obj) {
        return false;
    }
    lv_obj_align(obj, align, x_ofs, y_ofs);
    return true;
}

int midea_ui_set_img_src(lv_obj_t* obj, char* name_str)
{
    if (obj == NULL || name_str == NULL) {
        return -1;
    }
    lv_img_set_src(obj, name_str);
    return 0;
}

int midea_ui_safe_remove_style_all(lv_obj_t* obj)
{
    if (!obj) {
        return -1;
    }
    lv_obj_remove_style_all(obj);
    return 0;
}

int midea_ui_safe_set_text(lv_obj_t * obj, char* text){

    if (obj == NULL || text == NULL) {
        return -1;
    }
    lv_label_set_text(obj,text);
    return 0;
}

void midea_ui_safe_del_obj_async(lv_obj_t * obj){
    if (!obj) {
        return;
    }
    lv_obj_del_async(obj);
}

char* midea_ui_safe_get_text(lv_obj_t * obj){
    if (obj == NULL) {
        return NULL;
    }
    if(lv_obj_get_class(obj) != &lv_label_class){
        return NULL;
    }
    return lv_label_get_text(obj);
}


int midea_ui_safe_del_timer(lv_timer_t *timer){
   if(timer == NULL){
      return -1;
   }
// #if CONFIG_SOC_BK7258
//     lv_vendor_disp_lock();
// #endif
   lv_timer_del(timer);
   timer = NULL;
// #if CONFIG_SOC_BK7258
//     lv_vendor_disp_unlock();
// #endif 
   return 0;
}

lv_timer_t *midea_ui_safe_create_timer(lv_timer_cb_t timer_xcb, uint32_t period, void * user_data){

// #if CONFIG_SOC_BK7258
//     lv_vendor_disp_lock();
// #endif
    lv_timer_t *tmp = lv_timer_create(timer_xcb,period,user_data);
// #if CONFIG_SOC_BK7258
//     lv_vendor_disp_unlock();
// #endif
    return tmp;
}

static uint8_t utf8_code_bytes(uint8_t byte)
{
    uint8_t bytes = 0;

    if (byte <= 0x7F) { // then ASCII 占用1个字节
        bytes = 1;
    } else if (byte >= 0xC0 && byte <= 0xDF) { // then 首字节   UTF-8 占用2个字节
        bytes = 2;
    } else if (byte >= 0xE0 && byte <= 0xEF) { // then 首字节   UTF-8 占用3个字节
        bytes = 3;
    } else if (byte >= 0xF0 && byte <= 0xF7) { // then 首字节   UTF-8 占用4个字节
        bytes = 4;
    } else if (byte >= 0xF8 && byte <= 0xFB) { // then 首字节   UTF-8 占用5个字节
        bytes = 5;
    } else if (byte >= 0xFC && byte <= 0xFD) { // then 首字节   UTF-8 占用6个字节
        bytes = 6;
    } else if (byte > 0x7F && byte < 0xC0) { // then UTF-8   非首字节
        bytes = 0;
    }

    return bytes;
}

int ui_util_get_wstr_len(char* name_str)
{
    int ret = 0;

    if (name_str) {
        int len = strlen(name_str);
        for (int i = 0; i < len;) {
            uint8_t bytes = utf8_code_bytes(name_str[i]);
            if (bytes == 0) {
                break;
            }
            ret++;
            i += bytes;
        }
    }
    return ret;
}

void ui_util_sub_pre_wstr(char* name_str, int toIndex, char** output)
{
    int utf8_character_cnt = 0;
    int origin_len = strlen(name_str);
    char* sub_str = (char*)malloc(origin_len + 1);
    memset(sub_str, 0, origin_len + 1);

    for (int i = 0; i < origin_len;) {
        uint8_t bytes = utf8_code_bytes(name_str[i]);
        if (bytes == 0) {
            break;
        }
        utf8_character_cnt++;
        i += bytes;
        if(utf8_character_cnt >= toIndex) {
            memcpy(sub_str, name_str, i);
            break;
        }
    }
    // printf("sub_str = %s\n", sub_str);
    *output = sub_str;
}

void ui_util_sub_subfix_wstr(char* name_str, int fromIndex, char** output)
{
    int utf8_character_cnt = 0;
    int origin_len = strlen(name_str);
    char* sub_str = (char*)malloc(origin_len + 1);
    memset(sub_str, 0, origin_len + 1);

    for (int i = 0; i < origin_len;) {
        uint8_t bytes = utf8_code_bytes(name_str[i]);
        if (bytes == 0) {
            break;
        }
        utf8_character_cnt++;
        i += bytes;
        if(utf8_character_cnt >= fromIndex) {
            strncpy(sub_str, &name_str[i], origin_len+1);
            break;
        }
    }
    // printf("sub_str = %s\n", sub_str);
    *output = sub_str;
}
